package com.guo.hui.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guo.hui.dao.blog.TpArticleMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.exception.BizException;
import com.guo.hui.model.blog.TpArticle;
import com.guo.hui.service.BlogArticleService;

@Service("blogArticleService")
public class BlogArticleServiceImpl implements BlogArticleService {

	@Autowired
	private TpArticleMapper articleDao;

	@Override
	public List<TpArticle> selectArticleByMap(Map<String, Object> map) {
		List<TpArticle> list = articleDao.selectByMap(map);
		return list;
	}

	@Override
	public void updateArticle(TpArticle tp) {
		if (tp.getId() != null) {
			articleDao.update(tp);
		} else {
			throw new BizException("文章ID获取失败,更新失败");
		}

	}

	@Override
	public void saveArticle(TpArticle tp) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		tp.setTime(format.format(new Date()));
		tp.setDelflag(DelFlag.AVIABLE.toString());
		articleDao.insert(tp);
	}

	@Override
	public void deleteArticle(int id) {
		TpArticle tp = articleDao.selectById(id);
		tp.setDelflag(DelFlag.LOSE.toString());
		articleDao.update(tp);
	}

}
